KISSY.add("curve",function(S){
	var Dom = S.DOM;
	/**
	*	传入一个数组，绘制曲线
	*/
	var default_config = {
		container:"container",
		width:540,
		height:300
		
		
	};
	var default_draw_config = {
		data : null,
		min : 0,
		max : 100,
		drawType : "all", //  'screen' | 'recode'
		dps : false, //dots per screen
		scroolPoint : 15
		
	};
	var default_recode_config = {
		dps : 20,//每个屏幕显示的数据量
		scroolPoint : 15, //开始滚动的位置
		//data : [-49,49], 
		min : -50,
		max : 50,
		dataIn : "manul", //输入方式 ：也可以是auto
		//if set to auto
		//dataSource : function, 
		drawType : "recode" // screen
		
	}
	var default_style = {
		bgcolor:"#182949",
		lineColor : "#f2f2f2",
		lineWidth : "1.5",
		gridColor : "#004ebc",
		gridTextColor : "#999",
		markColor : "#3a7dda",
		lineJoin : "round",
		padding : 5,
		showGridText : true,
		showPointText : true
	}
	var CurveDrawer = function(cfg){
		cfg = S.mix(cfg,default_config,false);
		
		var t = this,
			ctx, 
			ifm,
			w = cfg.width,
			h = cfg.height,
			elc = Dom.get(cfg.container);
			
		
		if(!elc) return false;
		
		
		S.mix(t,{
			_ready : false,
			_ready_fnlist : [],
			el_c : elc,
			style : S.mix({}, default_style)
		})
		
		
		
		//t._tracedata = null;
		
		
		if(S.UA.ie){
			ifm = Dom.create("<iframe style='border:0'>",{
				width : w,
				height : h,
				scrolling : "no"
			});
			ifm.setAttribute("frameborder", "0", 0);
			elc.appendChild(ifm);
			ifm.src = "vmlcontainer.htm?cache="+new Date().getTime();
			//vmlText in iframe can not be seen
			// ifm.src = "testVML.php?cache="+new Date().getTime();
			// return;
			ifm.onload = function(){
				var win2 = ifm.contentWindow;
				setTimeout(function(){
					if(win2.getContext){
						t.ctx = win2.getContext(w,h);
						t._ready = true;
						t.ready();
					}else{
						setTimeout(arguments.callee,20)
					}
				},20);
			};
		}else{
			t.cv = Dom.create(
					"<canvas width=\""+w+"\" height=\""+h+"\">"
				);
			S.one(elc).append(t.cv);
			if( !t.cv.getContext){
				alert("Canvas not Support");
				return;
			}
			
			t.ctx = t.cv.getContext("2d");
			t._ready = true;
			t.ready();
		}
		t.cfg = cfg;
		//var g = getCanvasContext();
		//g.translate(cfg.width/2, cfg.height/2);
	};
	
	
	S.mix(CurveDrawer.prototype, {
		ready : function(fn){
			if(S.isFunction(fn)){
				this._ready_fnlist.push(fn);
			}
			if(this._ready && this._ready_fnlist.length > 0) {
				fn = this._ready_fnlist.shift();
				fn();
				if(this._ready_fnlist.length > 0) this.ready();
			}
		},
		/**
		 * 清空画布
		 */
		clear : function(){
			var t = this,
				cfg = t.cfg;
			t.ctx.fillStyle = t.style.bgcolor;
			//t.ctx.clearRect(0,0,cfg.width,cfg.height);
			t.ctx.fillRect(0,0,cfg.width,cfg.height);
		},
		/**
		 * 划线
		 * data	：源数据
		 * max	: 刻度高点
		 * min	: 刻度低点
		 */
		draw : function(cfg){
			var cfg = S.mix(cfg,default_draw_config,false),
				t = this,
				max,min,v,h, i,l,
				data = cfg.data,
				dlength,
				ctx = t.ctx,
				h_start,
				y0,
				rgn = t.getRegn(),
				kedu = [], dots = [];
			
			t.clear();
			//处理显示数据子集
			if(cfg.drawType == "screen" && data.length > cfg.dps){
				dlength = (data.length - cfg.dps) % (cfg.dps - 1) + 1; 
				if(dlength === 1){
					dlength = cfg.dps;
				}
			}else if(cfg.drawType == "recode" && data.length > cfg.scroolPoint){
				dlength = cfg.scroolPoint;
			}else{
				dlength = data.length;
				cfg.dps = (!cfg.dps)?dlength:cfg.dps;
			}
			
			h_start = data.length - dlength;
			data = data.slice(data.length - dlength, data.length);
			
			
			//获取最大值与最小值
			max = min = data[0];
			for (i = 0; i<dlength; i++){
				if(!S.isNumber(data[i])) {
					alert("arr provide is must contain Numbers only!");
					return;
				}
				
				if(i === 0) {
					max = data[i];
					min = data[i];
				}else{
					max = Math.max(data[i],max);
					min = Math.min(data[i],min);
				}
			}
			max = cfg.max || max;
			min = cfg.min || min;
			
			//获得x y转化因子 h, v
			v = rgn.height / (max - min);
			h = rgn.width / (cfg.dps - 1);
			y0 = rgn.bottom + min * v;
			
			//0 刻度
			kedu.push({
				y : y0,
				value : 0,
				type : "zero"
			});
			
			//所有点
			for (i = 0; i<dlength; i++){
				dots.push({
					value : data[i],
					x: rgn.left + h*i,
					y: y0 - data[i] * v
				});
			}
			
			// 所有横坐标
			for(i = 0, l = cfg.dps; i<l; i++){
				kedu.push({
					x : rgn.left + h*i,
					value : h_start + i,
					type : "normal"
				});
			}
			
			//画刻度
			t.kedu(kedu);
			
			// 画折线
			t.drawTheLine(dots);
			
			
			
		},
		
		
		/**
		 *	动态记录
		 */
		startRecode : function(cfg){
			var rc = S.mix(cfg,default_recode_config,false);
			S.mix(rc,{data : []}, false);
			this._recode_cfg = rc;
			this._recode_listening = true;
			this.clear();
		},
		
		recode : function(income){
			var rc = this._recode_cfg;
			if(!this._recode_listening) return;
			rc.data.push(income);
			this._refresh();
		},
		
		_refresh : function(){
			var rc = this._recode_cfg;
			var drawcfg = {
				data : rc.data,
				drawType : rc.drawType,
				dps : rc.dps,
				min : rc.min,
				max : rc.max,
				scroolPoint : rc.scroolPoint
			};
			this.draw(drawcfg);
		},
		
		// 画线
		drawTheLine : function(dots){
			var t = this,i,l,
				ctx = t.ctx;
			ctx.beginPath();
			ctx.lineWidth = t.style.lineWidth;
			ctx.strokeStyle  = t.style.lineColor;
			ctx.lineJoin  = t.style.lineJoin;
			for(i = 0, l = dots.length; i<l; i++){
				if(i == 0){
					ctx.moveTo(dots[i].x,dots[i].y);
				}
				ctx.lineTo(dots[i].x,dots[i].y);
				if(t.style.showPointText ) t.markText(dots[i].value, dots[i].x,dots[i].y);
			}
			ctx.stroke();
		},
		
		//写字
		markText : function(text,x,y,fillStyle){
			var t = this,
				cfg = t.cfg,
				ctx = t.ctx;
				
			if(x > cfg.width - 20){
				x = x - 20;
			}
			
			if(y > cfg.height - 10){
				y = cfg.height - 10;
			}else if( y < 10 ){
				y += 15;
			}
			
			ctx.fillStyle = (S.isUndefined(fillStyle))?t.style.markColor:fillStyle;
			ctx.fillText(text,x,y);
		},
		
		//刻度线
		kedu : function(data){
			var t = this,
				ctx = t.ctx,
				cfg = t.cfg,
				padding = t.style.padding,
				width = cfg.width,
				height = cfg.height,
				group = {},
				i,l,line, lines;
			//drowGrids
			
			for(i = 0,l = data.length; i < l; i++ ){
				line = data[i]
				if(!"type" in line){
					line.type = "normal";
				}
				
				if (!(line.type in group)){
					group[line.type] = [];
				}
				group[line.type].push(line);
			}
			for(lines in group){
				if(group.hasOwnProperty(lines)){
					ctx.beginPath();
					ctx.strokeStyle  = t.style.gridColor;
					ctx.lineWidth  = (lines == "normal") ? 0.5 : 2;
					t.drawKedu(group[lines]);
					ctx.stroke();
				}
			}
		},
		
		//划刻度
		drawKedu : function(line){
			var t = this,i, l;
			if(S.isArray(line)){
				for(i =0, l = line.length; i < l; i++){
					arguments.callee.call(t, line[i]);
				}
				return;
			}
			
			var rgn = t.getRegn(),
				ctx = t.ctx;
			if("x" in line){
				ctx.moveTo(line.x, rgn.top);
				ctx.lineTo(line.x, rgn.bottom);
				if(t.style.showGridText) t.markText(line.value,line.x,rgn.bottom, t.style.gridTextColor); 
			}else if ("y" in line){
				ctx.moveTo(rgn.left, line.y);
				ctx.lineTo(rgn.right, line.y);
				if(t.style.showGridText) t.markText(line.value,rgn.left,line.y, t.style.gridTextColor);
			}
		},
		
		//边界
		getRegn : function(){
			var t = this,
				cfg = t.cfg,
				p = t.style.padding,
				w = cfg.width,
				h = cfg.height;
			return {
				left : p,
				right : w - p,
				top : p,
				bottom : h - p,
				width : w - 2*p,
				height : h - 2*p
			}
		},
		
		setStyle : function(stylecfg){
			S.mix(this.style, stylecfg);
		}
	});
	S.CurveDrawer = CurveDrawer;
});
